<?php
/*
 * Created on 3 mars 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class ConnexionController extends Zend_Controller_Action
{
	
	public function init()
		{
		/* Initialize action controller here */
		}
	
	public function indexAction()
		{
		$this->view->title = "Page de connexion";
		$this->view->headTitle($this->view->title);
		$request = $this->getRequest();
		if($request->getParam('demo')){
			$this->view->demo = "demo";
		}
		if($request->isPost()){
				$username =$this->getRequest()->getParam('user_login');
				$password=$this->getRequest()->getParam('user_pass');
				$authAdapter = $this->getAuthAdapter();
        
                $authAdapter->setIdentity($username)
                            ->setCredential($password);
                         
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($authAdapter);
                
				//$result = (($username =='demo')and ($password=='demo'))?true:false;        
				
				if($result->isValid()){
                    $identity = $authAdapter->getResultRowObject();
                    $authStorage = $auth->getStorage();
                    $authStorage->write($identity);
					$this->_redirect('client');
				} else {
					$this->view->errorMessage = "Le login ou le mot de passe est erron&eacute;.";
				}
		}
		
		
		}
	
	
	public function logoutAction(){
    	$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		Zend_Auth::getInstance()->clearIdentity();
      	$this->_redirect('index');
    	
    }
	
	private function getAuthAdapter() {
		
		$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
		$authAdapter->setTableName('user')
		->setIdentityColumn('usr_login')
		->setCredentialColumn('usr_pasword');
		return $authAdapter;
	}
	
	
}


